import os
import sys
from PyQt5.QtWidgets import QMainWindow, QWidget, QStackedWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel
from PyQt5.QtCore import Qt
from controller import Controller
from data_manager import ensure_data_files, load_messages
from ui.heroes_view import HeroesView
from ui.battles_view import BattlesView
from ui.spirit_view import SpiritView
from ui.peace_view import PeaceView
from ui.message_view import MessageView

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("抗战记忆可视化系统 · Memory of Resistance")
        self.resize(1200, 800)
        self.setStyleSheet(
            """
            QMainWindow { background: #161616; }
            QLabel { color: #f5f5f5; font-family: 'SimSun','Noto Sans SC','Microsoft YaHei UI'; }
            QPushButton { background: #8e1616; color: #fff; border: 1px solid #b71c1c; padding: 8px 14px; }
            QPushButton:hover { background: #a61a1a; }
            QPushButton:pressed { background: #7a1212; }
            """
        )

        self.controller = Controller()

        # Load or create data and assets
        self.data = ensure_data_files()

        central = QWidget()
        self.setCentralWidget(central)
        layout = QVBoxLayout(central)

        # Top navigation
        nav = QHBoxLayout()
        self.btn_heroes = QPushButton("英雄人物")
        self.btn_battles = QPushButton("重大战役")
        self.btn_spirit = QPushButton("抗战精神")
        self.btn_peace = QPushButton("和平愿景")
        self.btn_messages = QPushButton("青年留言")
        for b in [self.btn_heroes, self.btn_battles, self.btn_spirit, self.btn_peace, self.btn_messages]:
            nav.addWidget(b)
        layout.addLayout(nav)

        # Pages
        self.pages = QStackedWidget()
        layout.addWidget(self.pages, 1)

        self.page_heroes = HeroesView(self.data.get("heroes", []))
        self.page_battles = BattlesView(self.data.get("battles", []))
        self.page_spirit = SpiritView(self.data.get("spirit", []))
        self.page_peace = PeaceView(self.data.get("peace", []))
        self.page_messages = MessageView()
        self.pages.addWidget(self.page_heroes)
        self.pages.addWidget(self.page_battles)
        self.pages.addWidget(self.page_spirit)
        self.pages.addWidget(self.page_peace)
        self.pages.addWidget(self.page_messages)

        # Bottom info
        self.footer = QLabel("传承红色基因 · 铭记历史 · 珍爱和平")
        self.footer.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.footer)

        # Wiring
        self.btn_heroes.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_heroes))
        self.btn_battles.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_battles))
        self.btn_spirit.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_spirit))
        self.btn_peace.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_peace))
        self.btn_messages.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_messages))
        self.pages.setCurrentIndex(4)

